package com.winning.cjfw.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Author: yanxf@winning.com.cn
 * Description: 加密工具类
 * Date 2019/06/03 15:50
 */
public class EnctyptUtil {

    private static final Logger logger = LoggerFactory.getLogger(EnctyptUtil.class);

    public static String unGzipString(String loggingString64, String charset) throws IOException {
        if (loggingString64 == null || loggingString64.isEmpty()) {
            return "";
        }
        StringWriter writer;
        // base 64 decode
        byte[] bytes64 = Base64.getDecoder().decode(loggingString64.getBytes(charset));
        // unzip to string encoding-wise
        try (ByteArrayInputStream zip = new ByteArrayInputStream( bytes64 )) {

            GZIPInputStream unzip = null;
            InputStreamReader reader = null;
            BufferedInputStream in = null;
            try {
                unzip = new GZIPInputStream( zip, 8192 );
                in = new BufferedInputStream( unzip, 8192 );
                // PDI-4325 originally used xml encoding in servlet
                reader = new InputStreamReader( in, charset );
                writer = new StringWriter();

                // use same buffer size
                char[] buff = new char[8192];
                for (int length; (length = reader.read( buff )) > 0; ) {
                    writer.write( buff, 0, length );
                }
            } finally {
                // close resources
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // Suppress
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // Suppress
                    }
                }
                if (unzip != null) {
                    try {
                        unzip.close();
                    } catch (IOException e) {
                        // Suppress
                    }
                }
            }
        }
        return writer.toString();
    }


    /**
     * 将字符串压缩后Base64
     * @param primStr 待加压加密函数
     * @param charset 字符编码
     * @return
     */
    public static String gzipString(String primStr, String charset) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }
        ByteArrayOutputStream out;
        GZIPOutputStream gout = null;
        try{
            out = new ByteArrayOutputStream();
            gout = new GZIPOutputStream(out);
            gout.write(primStr.getBytes(charset));
            gout.flush();
        } catch (IOException e) {
            logger.error("对字符串进行加压加密操作失败：", e);
            return null;
        } finally {
            if (gout != null) {
                try {
                    gout.close();
                } catch (IOException e) {
                    logger.error("对字符串进行加压加密操作，关闭gzip操作流失败：", e);
                }
            }
        }
        return new BASE64Encoder().encode(out.toByteArray());
    }


}
